<?php
/**
 * Created by allen.qiang.
 * 拿破仑莓恋预售ajax
 * Date: 14-12-01
 * @author allen.qiang <allen.qiang@fistforward.com>
 * FUN-AC-0022
 */
quanli_require('module/func/eai.func.php');
quanli_require('module/base/goods/quanliGoods.class.php');
quanli_require('module/base/activity/activityConfig.class.php');//活动配置文件
quanli_include('module/base/promotion/quanliCoupon.class.php');//加载优惠券类
quanli_require('module/base/activity/activityConfig.class.php');//活动配置文件


class ajax_sale{

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1://立即订购选择的磅数
                    $this->check();
                    break;
                case 2://用户免单信息
                    $this->free();
                    break;
            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    function check(){
        global $G_SHOP;
//        if(!$G_SHOP->currentPCustID){
//            returnJsonData("请登录", 400);
//        }
        $number=$_REQUEST['number'];//选中的磅数
        if( !in_array($number,array(0,1,2,3))){
            returnJsonData("参数错误", 400);
        }
        $amount=$_REQUEST['amount'];//数量
        $activity_config = new activityConfig('pre_sale'); //活动配置
        $config_array = $activity_config->config;
        //实例化商品类
        $goodsHandler = new quanliGoods( $G_SHOP->DBCA ) ;
        $goodsEdit=$config_array['goodsEdit'];//活动商品SKU 基础设置ID
        $sys_sql="SELECT itemName FROM sys_item WHERE itemCategoryID=?";
        $sys_query=$G_SHOP->DBCA->stmt_query($sys_sql,'s',$goodsEdit);
        foreach($sys_query as $sys_array ){
            $goods_array=array(
                'product_code' => $sys_array->itemName,    //商品sku
                'cityID'     => $G_SHOP->get_cookie_info('EBSIG_CITY_ID'), //城市ID
                'forsale'   => 1                                //上架
            );
            $goods_spec[]=$goodsHandler->searchGoodsSale($goods_array);

        }
        $buyGoods=array(
            'postID'=>$goods_spec[$number][0]['postID'],
            'goods_amount'=>$amount
        );
        returnJsonData($buyGoods, 200);
    }


    function free(){
        global $G_SHOP;
        //获取旋梭日期
        $beginDate=$_REQUEST['sendDate']." 00:00:00";//开始日期
        $endDate=$_REQUEST['sendDate']." 23:59:59";//结束日期
        $checkTime=date("Y-m-d",strtotime("-1 day"));//前一天日期
        $checkTime1=$checkTime." 23:59:59"; //前一天的日期时间

        $activity_sql="SELECT extend_5,extend_6 FROM activities_extend WHERE activityNumber=? and extend_2 > ? and extend_2 < ? and extend_2 <  ? LIMIT 10";
        $activity_query=$G_SHOP->DBCA->stmt_query($activity_sql,'ssss',33000,$beginDate,$endDate,$checkTime1);
        if(!empty($activity_query)){
            foreach($activity_query as $activity_array){
                $free[]=array(
                    'number'=>substr_replace($activity_array->extend_5,'****',3,4),
                    'usename'=>$activity_array->extend_6,
                );
            }
        }
        //如果所选的日期等于当前日期前一天的日期 则当前日期的14点清空数据
        if(trim ($_REQUEST['sendDate'])==trim($checkTime)){
            $checkTime5=date("H:i:s");//获取当天的时间 （14:00:00）
            if($checkTime5<"14:00:00")
                $free=array();
        }
        if(!empty($free)){
            returnJsonData($free, 200);
        }else{
            returnJsonData('准时公布', 400);
        }

    }

}